package com.brand.prisd.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.brand.prisd.R;
import com.brand.prisd.utils.DensityUtil;

/**
 * Created by HJ on 2018/12/13.
 */
public class CircleProgressBar extends View {

    private final int DEFAULT_PROGRESS = 50;
    private final int DEFAULT_MAX = 100;
    private final int DEFAULT_HintColor = Color.GRAY;
    private final int DEFAULT_ProgressColor = Color.BLUE;
    private final int DEFAULT_percentage_progress_color = Color.BLACK;
    private int progress;
    private int max;
    private int hintColor;
    private int progressColor;
    private String percentage_progress;
    private int percentage_progress_color;
    private static final int DEFAULT_WIDTH = 0;
    private static final int DEFAULT_HEIGHT = 0;
    private Paint paint;
    private Paint percentagePaint;
    private RectF rectF;
    private Paint arcPaint;
    private float baseline;
    private int textSize;
    private Rect bounds;

    public CircleProgressBar(Context context) {
        super(context);
    }

    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        progress = typedArray.getInteger(R.styleable.CircleProgressBar_progress, DEFAULT_PROGRESS);
        max = typedArray.getInteger(R.styleable.CircleProgressBar_max, DEFAULT_MAX);
        hintColor = typedArray.getColor(R.styleable.CircleProgressBar_progressHint, DEFAULT_HintColor);
        progressColor = typedArray.getColor(R.styleable.CircleProgressBar_progressColor, DEFAULT_ProgressColor);
        percentage_progress = typedArray.getString(R.styleable.CircleProgressBar_percentage_progress);
        percentage_progress_color = typedArray.getColor(R.styleable.CircleProgressBar_percentage_progress_color, DEFAULT_percentage_progress_color);
        typedArray.recycle();
        initPaint();
        rectF = new RectF();
        bounds = new Rect();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(hintColor);
        paint.setStyle(Paint.Style.STROKE);


        arcPaint = new Paint();
        arcPaint.setAntiAlias(true);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setColor(progressColor);


        percentagePaint = new Paint();
        percentagePaint.setStyle(Paint.Style.FILL);
        percentagePaint.setColor(percentage_progress_color);
        percentagePaint.setStrokeWidth(5);
        textSize = DensityUtil.dip2px(getContext(), 20);
        percentagePaint.setTextSize(textSize);
        baseline = getBaseline(percentagePaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int resultWidth = getSize(DEFAULT_WIDTH, widthMeasureSpec);
        int resultHeight = getSize(DEFAULT_HEIGHT, heightMeasureSpec);
        setMeasuredDimension(resultWidth, resultHeight);
        cx = getMeasuredWidth() / 2;
        cy = getMeasuredHeight() / 2;
        minSide = Math.min(getMeasuredWidth(), getMeasuredHeight());
        strokeWidth = minSide / 16;
        paint.setStrokeWidth(strokeWidth);
        arcPaint.setStrokeWidth(strokeWidth);
        r = minSide / 2 - strokeWidth / 2;
        rectF.set(strokeWidth / 2, strokeWidth / 2, getMeasuredWidth() - strokeWidth / 2, getMeasuredHeight() - strokeWidth / 2);
    }

    private int getSize(int defaultSize, int measureSpec) {
        int result = defaultSize;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.AT_MOST:
                result = defaultSize;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }
        return result;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        sweepAngle = progress;
//        Logger.i("sweepAngle: " + sweepAngle);
        percent = (int) (progress / 3.6);

        if (getMeasuredWidth() == DEFAULT_WIDTH || getMeasuredHeight() == DEFAULT_HEIGHT)
            return;
        invalidate();
    }

    private int cx;
    private int cy;
    private int r;
    private int minSide;
    private int strokeWidth;
    private float sweepAngle = 0;
    private int percent;

    /**
     * 计算绘制文字时的基线到中轴线的距离
     *
     * @return 基线和centerY的距离
     */
    public static float getBaseline(Paint p) {
        Paint.FontMetrics fontMetrics = p.getFontMetrics();
        return (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(cx, cy, r, paint);
        canvas.drawArc(rectF, -90, sweepAngle, false, arcPaint);
        String text = percent + "%";
        percentagePaint.getTextBounds(text,0,text.length(),bounds);
        int textWidth = bounds.width();
        canvas.drawText(text, cx -textWidth/2 , cy + baseline, percentagePaint);
    }
}
